package com.suli.account.controller;


import com.scenic.util.ResponseResult;
import com.suli.account.entity.RealTimeAnnounce;
import com.suli.account.service.RealTimeAnnounceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-04-17
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/realTimeAnnounce")
public class RealTimeAnnounceController {

    @Autowired
    RealTimeAnnounceService announceService;

    /**
     * 发布实时公告
     * @param announce 实时公告
     * @return 发布结果
     */
    @PostMapping("/publishReal")
    public ResponseResult publishReal(@RequestBody RealTimeAnnounce announce){
        announceService.save(announce);
        return ResponseResult.ok().message("发布成功");
    }

    /**
     * 分页获取实施公告发布记录
     * @param limit 数量
     * @param page 页码
     * @return 公告发布记录
     */
    @GetMapping("/getReal/{page}/{limit}")
    public ResponseResult getReal(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> realAnnounce = announceService.getRealAnnounce(page, limit);
        if(ObjectUtils.isEmpty(realAnnounce))
        return ResponseResult.error().message("拉取实时公告列表失败");
        return ResponseResult.ok().data(realAnnounce);
    }
    @DeleteMapping("/deleteReal")
    public ResponseResult deleteReal(@RequestBody List<Integer> ids){
        if(announceService.removeByIds(ids)){
            return ResponseResult.ok().message("删除成功");
        }
        return ResponseResult.error().message("删除失败");
    }

}

